<?php

/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/18
 * Time: 17:06
 */
class Utils
{
    public static function getCsv($filename)
    {
        setlocale(LC_ALL, 'zh_CN');
        $path = dirname(__DIR__) . '/csv/' . $filename . '.csv';
        $file = fopen($path, 'r');
        while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
            $lists[] = $data;
        }

        unset($lists[0]);
        return $lists;
        fclose($file);
    }


    public static function getKey($val, $filename)
    {
        $rt = '';
        $csvArray = self::getCsv($filename);
        foreach ($csvArray as $k => $v) {
            if ($val == $v[1]) {
                $rt = $v[0];
                break;
            }
        }


        return $rt;
    }

    public static function getValue($key, $filename)
    {
        $rt = '';
        $csvArray = self::getCsv($filename);
        foreach ($csvArray as $k => $v) {
            if ($key == $v[0]) {
                $rt = $v[1];
                break;
            }
        }

        return $rt;
    }


    public static function check_email($email)
    {
        $reg = '/^([a-zA-Z0-9]{1,20})(([_-.])?([a-zA-Z0-9]{1,20}))*@([a-zA-Z0-9]{1,20})(([-_])?([a-zA-Z0-9]{1,20}))*(.[a-z]{2,4}){1,2}$/';
        if (preg_match($reg, $email))
            return true;
        return false;
    }
}
